<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData" @buttonClick="handleAdd()"></card-header>
      <div class="card-body">
        <avue-crud :data="messageData"
                   :option="messageoption"
                   :page="messagepage"
                   :table-loading="tableLoading"
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   @refresh-change="refreshChange"
                   @search-change="searchChange"
                   @search-reset="refreshChange">
          <template slot-scope="scope" slot="menu">
            <div class="menu-title" @click.stop="haveReadContent(scope.row)" v-if="scope.row.haveRead==false">
              <i class="iconfont iconalter icon-warning"></i>
              <p class="menu-title-text">标为已读</p>
            </div>
          </template>
        </avue-crud>
      </div>
    </div>
  </basic-container>
</template>
<script>
  import {getunreadMsg,toHaveRead} from "@/api/system";
  import {SYS_DIC} from '@/const/dict'
  export default {
    name: "toDoMsg",
    data() {
      return {
        headerData: {
          title: '通知消息'
        },
        tableLoading: false,
        unreadFlag:false,//是否未读标志
        messagepage: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        messageData: [],
        messageoption: {
          menuWidth:130,
          page: true,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [
            {
              label: "消息内容",
              prop: "content",
              cell: true,
              formatter: function (row, value, label, column) {
                if(row.module=='4'){
                return '<a style="cursor: pointer;" onclick="messageContent(\''+row.messageId + '\',\''+row.businessKey + '\')">'+value+'</a>';
                }else if(row.module=='6'){
                return '<a style="cursor: pointer;" onclick="messageContente(\''+row.messageId + '\')">'+value+'</a>';
                }else{
                  if(row.type=='1'){
                    return '<a style="cursor: pointer;" onclick="detailApplication(\''+row.formKey + '\')">'+value+'</a>';
                  }else{
                    return '<a style="cursor: pointer;" onclick="messageContent(\''+row.messageId + '\')">'+value+'</a>';
                  }
                }

              },
            },{
              label: "消息内容",
              prop: "contentLike",
              search: true,
              hide: true,
              cell: true,
              formatter: function (row, value, label, column) {
                if(row.module=='4'){
                  return '<a style="cursor: pointer;" onclick="messageContent(\''+row.messageId + '\',\''+row.businessKey + '\')">'+value+'</a>';
                }else if(row.module=='6'){
                  return '<a style="cursor: pointer;" onclick="messageContente(\''+row.messageId + '\')">'+value+'</a>';
                }else{
                  if(row.type=='1'){
                    return '<a style="cursor: pointer;" onclick="detailApplication(\''+row.formKey + '\')">'+value+'</a>';
                  }else{
                    return '<a style="cursor: pointer;" onclick="messageContent(\''+row.messageId + '\')">'+value+'</a>';
                  }
                }

              },
            },
            {
              label: "发送时间",
              prop: "sendTime",
              width: 120,
              cell: true,
              formatter: function (row, value, label, column) {
                if(row.sendTime!=null){
                  var data = row.sendTime.split(' ');
                  return data[0];
                }
              },
            },
            {
              label: "是否已读",
              width: 120,
              prop: "haveRead",
              dicData:SYS_DIC.MSGREAD,
              cell: true
            },
          ]
        },
        contentLikes:''
      };
    },
    created(){
      var self = this;
      window.messageContent= self.messageContent;
      window.messageContente= self.messageContente;
      window.detailApplication= self.detailApplication;
      this.unreadFlag = this.$route.params.id;
      if(this.unreadFlag =='true'){
        this.headerData.title='历史消息'
      }
      this.getMessageList(this.messagepage);
    },
    methods: {
      // 查看
      detailApplication(formKey){
        let src = formKey;
        this.$router.push({
          path: src
        })
      },
      // 获取消息列表信息
      getMessageList(page, params){
        this.tableLoading = true;
        getunreadMsg(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          haveRead:this.unreadFlag,
        },params)).then(response => {
          if(response.data.code == '0'){
            this.messageData = response.data.data.records;
            this.messagepage.total = response.data.data.total;
            this.$emit("getTotal", response.data.data.total);
            this.tableLoading = false;
          }
        })
      },

      /**
       * 刷新回调
       */
      refreshChange() {
        this.contentLike= '';
        this.getMessageList(this.messagepage)
      },
      //搜索回调
      searchChange(params){
        this.messagepage.currentPage = 1
        this.getMessageList(this.messagepage,this.dealSearchParam(params))
      },
      // 处理搜索过滤
      dealSearchParam(params) {
        if (params.contentLike) {
          params.contentLike = params.contentLike
        }else{
          params.contentLike =""
        }
        this.contentLikes=  params.contentLike;
        return params;
      },
      currentChange(currentPage) {
        var page = this.messagepage;
        page.currentPage = currentPage;
        var params={contentLike: this.contentLikes};
        this.getMessageList(page, params);
      },
      sizeChange(pageSize) {
        var page = this.messagepage;
        page.pageSize = pageSize;
        var params={contentLike: this.contentLikes};
        this.getMessageList(page, params);
      },
      //未读消息点击详情
      messageContent(messageId,businessKey){
        let src = `/unreadMsgcontent/detail/${messageId}`;
        this.$router.push({
          path: src,
          query:{
            businessKey:businessKey
          }
        })
      },
      //下达方案消息点击详情
      messageContente(messageId){
        let src = `/unreadMsgcontent/detail/${messageId}`;
        this.$router.push({
          path: src,
          query:{
            release:true
          }
        })
      },
      //已读消息点击详情
      haveReadContent(row){
        this.$confirm("确定将此条消息标为已读吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          toHaveRead(Object.assign({
            messageIds: row.messageId
          })).then(response => {
            if(response.data.code == 0){
              this.$notify({
                title: '成功',
                message: '调整成功',
                type: 'success',
                duration: 2000
              });
              this.getMessageList(this.messagepage);
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消标记'
          });
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .card-border {
    border: 1px solid #d2d2d2;
  }
  .card-body {
    padding: 10px;
  }
</style>
